<template>
  <div class="container_edit container">
    <div class="header">
      <p>轮播图管理>>编辑轮播图</p>
      <el-button
        type="primary"
        @click="back"
      >返回</el-button>
    </div>
    <div class="main">
      <el-form
        :model="user"
        :rules="rules"
        ref="user"
        label-width="150px"
        class=""
      >
        <el-form-item
          label="轮播图："
          prop="headPortrait"
        >
          <el-upload
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
           >
            <img
              v-if="imageUrl"
              :src="imageUrl"
              class="avatar"
            >
            <i
              v-else
              class="el-icon-plus avatar-uploader-icon"
            ></i>
          </el-upload>
        </el-form-item>
        <el-form-item
          label="关联办公室："
          prop="outId"
        >
          <el-button
            type="primary"
            @click="selectBtn()"
          >{{info&&info.name||'请选择'}}</el-button>
        </el-form-item>
        <div v-if="info" class="selectInfo"><span>{{info.name}}</span><span>{{info.unitPrice}}</span><span>{{info.totalPrice}}</span><span>{{info.buildingName}}</span><span>{{info.isSale == 1?'上架':'下架'}}</span><span>{{info.isRecommend == 1?'是':'否'}}</span></div>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSave('user')"
          >保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog title="关联办公室" :visible.sync="dialogVisible" width="80%" >
       <div class="input_modal">
        <div class="input-item">
          <span>办公室名称：</span>
          <el-input v-model="name" placeholder="请输入内容" class="proName" @keyup.enter.native="search"></el-input>
        </div>
        <div class="input-item">
          <el-button icon="el-icon-search" type="primary" size="medium" @click="getList">查询</el-button>
        </div>
      </div>
      <div class="table_con">
        <el-table v-loading="loading" :data="tableData" :border='true'>
          <el-table-column :formatter="formatter" prop="name" label="办公室名称"></el-table-column>
          <el-table-column :formatter="formatter" prop="unitPrice" label="单价"></el-table-column>
          <el-table-column :formatter="formatter" prop="totalPrice" label="总价"></el-table-column>
          <el-table-column :formatter="formatter" prop="buildingName" label="所属写字楼"></el-table-column>
          <el-table-column :formatter="formatter" prop="isSale" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.isSale == 0">下架</div>
              <div v-else-if="scope.row.isSale == 1">上架</div>
            </template>
          </el-table-column>
          <el-table-column :formatter="formatter" prop="isRecommend" label="是否是首页推荐">
            <template slot-scope="scope">
              <div v-if="scope.row.isRecommend == 0">否</div>
              <div v-else-if="scope.row.isRecommend == 1">是</div>
            </template>
          </el-table-column>
          <el-table-column prop="" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="handleRecommend(scope.row)">选取</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageIndex" :page-size="pageSize" :page-sizes='[10,20,30,40]' layout="total, sizes, prev, pager, next, jumper" :total="pageCount">
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      tableData: [],
      pageCount: null,
      pageIndex: 1,
      pageSize: 10,
      loading: true,
      name: "",
      dialogVisible:false,
      upLoadUrl: "",
      imageUrl: "",
      user: {
        outId: "",
        coverKey: "",
        id: ""
      },
      info:null,
      rules: {
        coverKey: [
          { required: true, message: "请选择轮播图", trigger: "blur" }
        ],
        outId: [
          { required: true, message: "请关联办公室", trigger: "blur" }
        ]
      },
    };
  },
  mounted() {
    this.id = this.$router.currentRoute.query.id;
    let sessionId = sessionStorage.getItem("sessionId");
    this.currentPage = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    this.upLoadUrl =
      CONSTANT.URL.SYSTEM.UPLOADFILEBYCOS + "?sessionId=" + sessionId;
    this.getList();
    if (this.id) {
      this.getInfo();
    }
  },
  components: {
    "bg-editor": () => import("../common/bg-editor.vue")
  },
  methods: {
    selectBtn(){
      this.dialogVisible = true
    },
    handleRecommend(info){
      console.log(info)
      this.info = info;
      this.dialogVisible = false
      this.user.outId = info.id
    },
    getList: function() {
        let url = CONSTANT.URL.ROOM.list;
        let data = {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          name: this.name,
        };
        this.loading = true;
        common.requestAjax(url, JSON.stringify(data), null, res => {
          this.loading = false;
          let data = res.data;
          this.tableData = data.bussData;
          this.pageCount = data.count;
        });
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.getList();
      },
      formatter(row, column, cellValue) {
        return cellValue ? cellValue : "- -";
      },
      handleCurrentChange(val) {
        this.pageIndex = val;
        this.getList();
      },
    getInfo: function() {
      let url = CONSTANT.URL.LIST.bannerByid;
      common.get(url,this.id, res => {
        if (res.status == 200) {
          let data = res.data.bussData;
          this.imageUrl = data.coverUrl
          this.user={
            outId: data.outId,
            coverKey: data.coverKey,
            id:data.id
          }
          this.info = data.officeRoom
        }
      });
    },
    handleSave: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let url = CONSTANT.URL.LIST.bannerSave;
          common.requestAjax(url, JSON.stringify(this.user), null, res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "保存成功！"
              });
              this.$router.replace({
                path: "/bannerList",
                query: { pageIndex: this.currentPage }
              });
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    beforeAvatarUpload(file) {
        console.log(file);
        let url = CONSTANT.URL.SYSTEM.UPLOADFILEBYCOS;
        let param = {
          suffix: file.name.split(".")[1], // 文件后缀  png
          contentType: file.type // 文件类型  image/png
        };
        common.requestAjax(url, '', param, res => {
          this.upLoadUrl = res.data.bussData.uploadUrl;
          this.user.coverKey = res.data.bussData.fileKey;
          common.uploadFile(this.upLoadUrl, file, file.type, res1 => {
            this.imageUrl = res.data.bussData.downloadUrl;
          })
        });
      },
    back() {
      this.$router.push({
        path: "/bannerList",
        query: { pageIndex: this.currentPage }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.selectInfo{
  margin-bottom: 30px;
  span{
    font-size: 14px;
    color: #606266;
    margin-left: 50px;
  }
}
.table-content {
  width: 500px;
  .table-item {
    position: relative;
    border: 1px solid #dcdfe6;
    i {
      position: absolute;
      right: -30px;
      top: 20px;
      cursor: pointer;
      font-size: 16px;
    }
    &:first-child {
      background-color: #409eff;
      color: #fff;
      border-right: 0px solid #dcdfe6;
      .table-list {
        padding: 0;
      }
    }

    .table-list {
      flex: 2;
      text-align: center;
      padding: 5px 0;
      .el-input {
        height: 70% !important;
        width: 90%;
      }
      &:first-child {
        flex: 3;
        border-right: 1px solid #dcdfe6;
      }
    }
  }
}
</style>
